package com.yeswater.multithread.chapter7;

import java.util.concurrent.TimeUnit;

/**
 * author : yeswater
 * date : 2023/11/6
 *
 * 组内的线程批量停止
 */

public class Run_7_2_6 {
    public static void main(String[] args) {
        try {
            ThreadGroup group = new ThreadGroup("group");

            for (int i = 0; i < 5; i++) {
                Thread1 thread = new Thread1(group, " 线程 " + (i + 1));
                thread.start();
            }
            TimeUnit.SECONDS.sleep(5);
            group.interrupt(); // 组内的线程批量停止
            System.out.println(" invoke interrupt");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static class Thread1 extends Thread {
        public Thread1(ThreadGroup group, String name) {
            super(group, name);
        }

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + "开始循环");

            while (!this.isInterrupted()) {

            }

            System.out.println(Thread.currentThread().getName() + "循环结束");
        }
    }
}
